package com.anjubao.parking.municipal.order.controller;

import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.order.service.ParkingStatOrderService;
import com.anjubao.parking.municipal.payment.constant.OrderStatusEnum;
import com.anjubao.parking.municipal.payment.dto.DateFeeDto;
import com.anjubao.parking.municipal.payment.dto.OrderPayChannelFeeDto;
import com.anjubao.parking.municipal.payment.dto.OrderRecordTotalDto;
import com.anjubao.parking.municipal.payment.dto.OrderZoneFeeDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单controller
 *
 * @author zhuchunfeng
 */
@RestController
@RequestMapping("/parking-stat-orders")
@Slf4j
@Api(value="Parkinglot-Order模块",tags = {"运行数据-支付统计"})
public class ParkingStatOrderController {

    @Resource
    private ParkingStatOrderService parkingStatOrdersService;

    /**
     * 【运营数据】-3  支付方式占比  参数【开始日期、结束日期】 默认 昨天-今天
     * parking_trade_order pay_channel_id  按天统计，where日期范围统计
     * /gateway/parking-order-api/parking-stat-orders/pay-channel-fee
     * @param cityId 城市ID
     * @param startTime 统计起始时间
     * @param endTime 统计结束时间
     * @return 支付百分比
     */
    @ApiOperation("支付方式占比")
    @GetMapping("/pay-channel-fee")
    public ApiResult<List<OrderPayChannelFeeDto>> queryParkingStatOrdersFee(@RequestParam(name = "cityId",required = true) Integer cityId,
                                                                            @RequestParam(name = "startTime", required = false) LocalDateTime startTime,
                                                                            @RequestParam(name = "endTime", required = false) LocalDateTime endTime) {
        if (null == startTime) {
            startTime = LocalDateTime.now().minusDays(100L);
        }
        if (null == endTime){
            endTime = LocalDateTime.now();
        }
        return ApiResult.success(parkingStatOrdersService.queryParkingStatOrdersFee(cityId, startTime, endTime));
    }

    /**
     *【运营数据】-4  停车记录总数  订单数量
     * parking_trade_order  charging_type=4免费  charging_type in 1,2,3 and status=1收费停车  status=0欠费停车
     * /gateway/parking-order-api/parking-stat-orders/total
     * 张原(张原) 04-24 09:10:45
     */
    @ApiOperation("停车记录总数")
    @GetMapping("/total")
    public ApiResult<OrderRecordTotalDto> queryParkingTotal(@RequestParam(name = "cityId",required = true) Integer cityId,
                                                            @RequestParam(name = "startTime", required = false) LocalDateTime startTime,
                                                            @RequestParam(name = "endTime", required = false) LocalDateTime endTime) {
        if (null == startTime) {
            startTime = LocalDateTime.now().minusDays(100L);
        }
        if (null == endTime){
            endTime = LocalDateTime.now();
        }
        return ApiResult.success(parkingStatOrdersService.queryParkingTotal(cityId, startTime, endTime));
    }

    /**
     * 【运营数据】-6 各区域收费排名 参数【开始日期、结束日期】 默认 今天-今天
     * parking_trade_order status=1 按地区统计，where日期范围统计
     * /gateway/parking-order-api/parking-stat-orders/zones-paid-fee
     */
    @ApiOperation("各区域收费排名")
    @GetMapping("/zones-paid-fee")
    public ApiResult<List<OrderZoneFeeDto>> queryZonesPaidFee(@RequestParam(name = "cityId",required = true) Integer cityId,
                                                              @RequestParam(name = "startTime", required = false) LocalDateTime startTime,
                                                              @RequestParam(name = "endTime", required = false) LocalDateTime endTime) {
        if (null == startTime) {
            startTime = LocalDateTime.now().minusDays(100L);
        }
        if (null == endTime){
            endTime = LocalDateTime.now();
        }
        return ApiResult.success(parkingStatOrdersService.queryZonesPaidFee(cityId, startTime, endTime, OrderStatusEnum.PAID.getCode()));
    }

    /**
     * 【运营数据】-7 各区域欠费排名 参数【开始日期、结束日期】 默认 今天-今天
     * parking_trade_order status=0 按地区统计，where日期范围统计
     * /gateway/parking-order-api/parking-stat-orders/zones-arrears-fee
     */
    @ApiOperation("各区域欠费排名")
    @GetMapping("/zones-arrears-fee")
    public ApiResult<List<OrderZoneFeeDto>> queryZonesArrearsFee(@RequestParam(name = "cityId",required = true) Integer cityId,
                                                            @RequestParam(name = "startTime", required = false) LocalDateTime startTime,
                                                            @RequestParam(name = "endTime", required = false) LocalDateTime endTime) {
        if (null == startTime) {
            startTime = LocalDateTime.now().minusDays(100L);
        }
        if (null == endTime){
            endTime = LocalDateTime.now();
        }
        return ApiResult.success(parkingStatOrdersService.queryZonesPaidFee(cityId, startTime, endTime, OrderStatusEnum.UN_PAID.getCode()));
    }


    /**
     【运营数据】-1 今日收费金额&昨日  参数【开始日期、结束日期】 默认 昨天-今天
     parking_trade_order total_fee status in(0,1) 按天统计，where日期范围统计
     /gateway/parking-order-api/parking-stat-orders/total-fee
     */
    @ApiOperation("今日收费金额&昨日")
    @GetMapping("/total-fee")
    public ApiResult<List<DateFeeDto>> queryTotalFee(@RequestParam(name = "startTime", required = false) LocalDate startTime,
                                                     @RequestParam(name = "endTime", required = false) LocalDate endTime)
    {
        if (null == startTime) {
            startTime = LocalDate.now().minusDays(100L);
        }
        if (null == endTime){
            endTime = LocalDate.now();
        }
        return ApiResult.success(parkingStatOrdersService.queryTotalFee(startTime,endTime));
    }







}
